<?php
namespace app\api\controller\cost;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\orderhawa\HawaList;
use app\api\controller\Base;
use app\common\model\user\User;
use app\common\model\cost\Costtype;
use app\common\model\cost\Pay;
use app\common\model\cost\Revenue;
use app\common\model\power\Rolepowerlnk;
use app\common\model\power\Rolepower;
class Costview extends Base
{
	/**
	 * 初始化
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(203008);
	}

	/**
	 * 获取订单列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'cost';
		$data = init_where($data);

		if(!empty($data['typeids'])){
			$data['typeids'] = explode(',',$data['typeids']);
			$data['where'][] = ['TypeId','in',$data['typeids']];
		}

		if(!empty($data['id'])){
			$data['where'][] = ['RelationUserId',$data['id']];
		}

		$str = '';//支出/收入
		$tableid = ''; //表id

		if($data['type']=='pay'){
			$list = Pay::usergetlist($data,'mana');
			$str = '支出';
			$tableid = 'PayId';
		}else{
			$list = Revenue::usergetlist($data,'mana');
			$str = '收入';
			$tableid = 'RevenueId';
		}

		foreach ($list['list'] as $key => $value) {
			if(empty($value['typeinfo']['TypeName'])){
				$list['list'][$key]['Name'] = '暂无';
			}else{
				$list['list'][$key]['Name'] = $value['typeinfo']['TypeName'];
			}
		}

		$list['tr']['title']  = [$str.'类别',$str.'时间','状态','查看',$str.'金额','记录时间'];
		$list['tr']['key'] = ['Name','PayTime','State',$tableid,'PayCost','UpTime'];
		$list['tr']['show']=[true,true,true,true,true,false];
		$list['tr']['type'] = CostType::getlist('costview');
		$list['tr']['typekey'] = ['id'=>'TypeId','text'=>'TypeName'];

		return myok($list);
	}


	/**
	 * 获取订单信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		$type = input('type');
		if(empty($id)||empty($type)){
			return myerr('缺少参数，请重新尝试');
		}
		if($type=='pay'){
			$data = Pay::getinfo($id,'costview');
		}else if($type=='revenue'){
			$data = Revenue::getinfo($id,'costview');
		}
		if(!$data['code']){
			return myerr($data['msg']);
		}
		$data = $data['msg'];
		
		if(!empty($data['RelationListId'])){
			if($data['typeinfo']['TypeTable']=='cost'){
				$data['tappage'] = 'cost/cost';
			}else{
				$user = User::getinfo();
				if($user->Role=='203'){
					$data['tappage'] = $data['typeinfo']['TypeTable'].'/'.$data['typeinfo']['TypeTable'].'acco';
				}else{
					$data['tappage'] = 'manaorder/mana'.$data['typeinfo']['TypeTable'];
				}
			}
			
		}
		$data['power'] = Rolepowerlnk::getpowerlnk('cost');
		$data['CELLPHONE'] = CELLPHONE;
		return myok($data);
	}
	

	
	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
